#coding: utf-8
'''
Condition 视频解帧
Data 2017-06-11https://kfm.kukewang.com/user/login
version:1.0
点击帧->页面加载帧中间有多少帧，再乘以每帧时长，就是启动时间
'''
import cv2 #开源的处理图像和视频的库
import win32ui #使py调用win工具
import sys,os

try:
    dlg = win32ui.CreateFileDialog(1) #1表示打开文件对话框
    dlg.SetOFNInitialDir('E:\pyCharm\jiezhen\lengqi') #设置打开文件对话框中的初始显示目录
    dlg.DoModal()
    full_name = dlg.GetPathName()
    
except Exception as e:
    print('请重新选择!~')

cap = cv2.VideoCapture(full_name)
if not cap.isOpened():
    print('无法打开视频文件')
    raw_input()
    sys.exit(0)

folder = 'E:\pyCharm\img\lengqi'

if not os.path.isdir(folder):
    os.mkdir(folder)
os.chdir(folder)

frmcnt = (int)(cap.get(cv2.CAP_PROP_FRAME_COUNT))
print('开始解析！！~loading')

temp = ''

for i in range(0,frmcnt-1):
    loading =  str(i*100/frmcnt)+'%'
    if loading != temp :
        print(loading)
    ret, frame = cap.read()
    cv2.imwrite("%6d.jpg"%i, frame)
    temp = loading

video_fps = cap.get(cv2.CAP_PROP_FPS)

video_time = frmcnt/video_fps

fps_time = video_time/frmcnt

print ('视频总帧数:' + str(frmcnt) + '帧')
print ('视频帧率:' + str(video_fps))
print ('视频总长度:' + str(video_time) + 's')
print ('每帧时长:' + str(fps_time) +'s')

print ('解帧已完成')
cap.release()
 